package com.generalbackend.common.model;

import com.generalbackend.common.enums.ReturnCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author tim.xie
 * @date 2021/11/17 16:44
 */
public class CommonResult<T> {

    public static final Logger LOG = LoggerFactory.getLogger(CommonResult.class);

    private int code = ReturnCodeEnum.SUCCESS.getValue();
    private String message = "成功";
    private String pageId = "etools";
    private T data;
    //存redis时，枚举类型被扁平映射到success上，无法还原对象：可以加success|直接返回（里面有类属性，可以加toString方法）|加success可以解决问题
    private Boolean success;

    public CommonResult() {
    }

    public CommonResult(T data) {
        this.data = data;
        if (data == null) {
            code = ReturnCodeEnum.FAIL.getValue();
            message = "失败!";
        }
    }

    public CommonResult(String message) {
        this.code = ReturnCodeEnum.FAIL.getValue();
        this.message = message;
    }

    public CommonResult(T data, String message) {
        this.data = data;
        this.message = message;
    }

    /**
     * 如果data为false则说明出错了
     *
     * @param data
     */
    public CommonResult(Boolean data) {
        if (!data) {
            code = ReturnCodeEnum.FAIL.getValue();
            message = "失败!";
        } else {
            code = ReturnCodeEnum.SUCCESS.getValue();
        }
    }

    public CommonResult(T data, ReturnCodeEnum rce) {
        this.data = data;
        this.code = rce.getValue();
    }

    public CommonResult(ReturnCodeEnum rce, String message) {
        this.message = message;
        this.code = rce.getValue();
    }

    public CommonResult(String checkName, String check, T resultCheck) {
        this.code = ReturnCodeEnum.FAIL.getValue();
        if (StringUtils.isEmpty(check)) {
            this.message = checkName + "不可为空";
        } else if (resultCheck == null) {
            this.message = "结果为null,可能有异常";
        } else {
            this.message = "成功";
            this.code = ReturnCodeEnum.SUCCESS.getValue();
            this.data = resultCheck;
        }
    }


    public boolean isSuccess() {
        return this.code == 0;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPageId() {
        return pageId;
    }

    public void setPageId(String pageId) {
        this.pageId = pageId;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static CommonResult successCommonResult(Object data, String message) {
        return new CommonResult(data, message);
    }

    public static CommonResult errorCommonResult(Object data, String message) {
        CommonResult commonResult = new CommonResult();
        commonResult.setData(data);
        commonResult.setCode(ReturnCodeEnum.FAIL.getValue());
        commonResult.setMessage(message);
        return commonResult;
    }

    public static CommonResult successCommonResult(String message) {
        CommonResult commonResult = new CommonResult();
        commonResult.setMessage(message);
        return commonResult;
    }

    public static CommonResult errorCommonResult(String message) {
        CommonResult commonResult = new CommonResult();
        commonResult.setMessage(message);
        commonResult.setCode(ReturnCodeEnum.FAIL.getValue());
        return commonResult;
    }


}
